#include "app_bluetooth.h"
#include <string.h>
#include <unistd.h>
#include "log/log.h"
#include "app_common.h"

static char send_buf[128];
static int send_offset = 0;
static char fix_header[] = {0xF1, 0xDD};

static void app_bluetooth_removeBuffer(int len)
{
    if (len > 0)
    {
        send_offset -= len;
        memcpy(send_buf, send_buf + len, send_offset);
    }
}

static int app_bluetooth_seekHeader()
{
    int index,result = -1;
    for (index = 0; index < send_offset - 3; index++)
    {
        if (memcmp(send_buf + index, fix_header, 2) == 0)
        {
            result = 1;
            break;
        }
        if (memcmp(send_buf + index, "OK", 2) == 0)
        {
            result = 0;
            break;
        }
    }
    app_bluetooth_removeBuffer(index);
    
    return result;
}

static int app_bluetooth_waitACK(Device *device)
{
    char buf[5];
    int len = read(device->fd, buf, 5);
    if (len != 4)
        return -1;
    if (memcmp(buf, "OK\r\n", 4) != 0)
        return -1;
    return 0;
}

int app_bluetooth_checkStatus(Device *device)
{
    write(device->fd, "AT\r\n", 4);
    return app_bluetooth_waitACK(device);
}

int app_bluetooth_setBaudRate(Device *device, BTBaudRate baud_rate)
{
    char buf[10];
    memcpy(buf, "AT+BAUD8\r\n", 10);
    memcpy(buf + 7, &baud_rate, 1);
    write(device->fd, buf, 10);
    return app_bluetooth_waitACK(device);
}

int app_bluetooth_setNetId(Device *device, char *net_id)
{
    unsigned char buf[14];
    memcpy(buf, "AT+NETID1111\r\n", 14);
    memcpy(buf + 8, net_id, 4);
    write(device->fd, buf, 14);

    return app_bluetooth_waitACK(device);
}

int app_bluetooth_setMAddr(Device *device, char *maddr)
{
    char buf[14];
    memcpy(buf, "AT+MADDR1111\r\n", 14);
    memcpy(buf + 8, maddr, 4);
    write(device->fd, buf, 14);

    return app_bluetooth_waitACK(device);
}

int app_bluetooth_reset(Device *device)
{
    write(device->fd, "AT+RESET\r\n", 10);
    return app_bluetooth_waitACK(device);
}

void app_bluetooth_preWrite(Device *device, void *ptr, int *p_len)
{
    char *buf = ptr;
    if (buf[0] != 2)
    {
        *p_len = 0;
        return;
    }
    if (buf[1] > 12)
    {
        *p_len = 0;
        return;
    }
    char temp[14];
    int len = buf[0] + buf[1];
    if (len +2 != *p_len)
    {
        *p_len = 0;
        return;
    }
    memcpy(temp, buf+2, len);

    memcpy(buf, "AT+MESH", 8);
    memcpy(buf + 8 + len, "", len);
    memcpy(buf + 8+len, "\r\n", 2);

    *p_len = 8 + len + 2;
    device->last_write_time = app_common_getCurrentTime();
}

void app_bluetooth_postRead(Device *device, void *ptr, int *p_len)
{
    memcpy(send_buf + send_offset, ptr, *p_len);
    send_offset += *p_len;
    *p_len = 0;
    if (send_offset < 4)
        return;
    int result = app_bluetooth_seekHeader();
    switch (result)
    {
    case 0:
        if (send_offset < 4)
            return;
        device->last_write_time = 0;
        app_bluetooth_removeBuffer(4);
        break;
    case 1:
        char *temp = ptr;
        if (send_buf[2] > send_offset -3)
            return;
        temp[0] = 2;
        temp[1] = send_buf[2] - 4;
        memcpy(temp + 2, send_buf + 3, 2);
        memcpy(temp + 4, send_buf + 7, temp[1]);
        *p_len = temp[0] + temp[1] + 2;
        app_bluetooth_removeBuffer(send_buf[2] + 3);
        break;
    default:
        break;
    }
}